#include <iostream>
#include <map>
#include <string>

// 产品工具抽象类
template <class AbstractProduct>
class AbstractProductTool {
public:
    virtual AbstractProduct* CreatProduct() = 0;

protected:
    AbstractProductTool() {}
    ~AbstractProductTool() {}
};

// 工厂模板类
template <class AbstractProduct>
class ProductFactory {
public:
    static ProductFactory<AbstractProduct>& instance() {
        static ProductFactory<AbstractProduct> factory;
        return factory;
    }

    void RegisterProduct(const std::string& name,
                         AbstractProductTool<AbstractProduct>* adsProductTool) {
        m_RegisterProductMap[name] = adsProductTool;
    }

    AbstractProduct* CreateProduct(const std::string& name) {
        if (m_RegisterProductMap.find(name) != m_RegisterProductMap.end()) {
            return m_RegisterProductMap[name]->CreatProduct();
        } else {
            return nullptr;
        }
    }

private:
    ProductFactory() {}
    ProductFactory(const ProductFactory&) {}
    ProductFactory& operator=(const ProductFactory&) {}

    std::map<std::string, AbstractProductTool<AbstractProduct>*>
        m_RegisterProductMap;
};

// 具体产品工具类
template <class AbstractProduct, class ConcreteProduct>
class ConcreteProductTool : public AbstractProductTool<AbstractProduct> {
public:
    explicit ConcreteProductTool(const std::string& name) {
        ProductFactory<AbstractProduct>::instance().RegisterProduct(name, this);
    }

    AbstractProduct* CreatProduct() override { return new ConcreteProduct(); }
};

// 利用全局静态变量的初始化特性实现产品类的自注册
#define RegisterProduct(AbstractProduct, ConcreteProduct) \
    static ConcreteProductTool<                           \
        AbstractProduct,                                  \
        ConcreteProduct> register##ConcreteProduct(#ConcreteProduct);

class Shoes {
public:
    virtual void show() = 0;
    virtual ~Shoes() = default;
};

class Nike : public Shoes {
public:
    void show() override { std::cout << "nike shoes\n"; }
};

class Adidas : public Shoes {
public:
    void show() override { std::cout << "adidas shoes\n"; }
};

class Clothe {
public:
    virtual void display() = 0;
    virtual ~Clothe() = default;
};

class Shirt : public Clothe {
public:
    void display() override { std::cout << "Shit\n"; }
};

class Dress : public Clothe {
public:
    void display() override { std::cout << "Dress\n"; }
};

int main() {
    // 注册Nike到Shoes的工厂类中
    RegisterProduct(Shoes, Nike)

        // 注册Adidas到Shoes的工厂类中
        RegisterProduct(Shoes, Adidas)

        // 注册Shirt类到Clothe的工厂类中
        RegisterProduct(Clothe, Shirt)

        // 注册Dress类到Clothe的工厂类中
        RegisterProduct(Clothe, Dress) Shoes* shoe1 =
            ProductFactory<Shoes>::instance().CreateProduct("Nike");
    Shoes* shoe2 = ProductFactory<Shoes>::instance().CreateProduct("Adidas");

    shoe1->show();
    shoe2->show();

    Clothe* clothe1 = ProductFactory<Clothe>::instance().CreateProduct("Shirt");
    Clothe* clothe2 = ProductFactory<Clothe>::instance().CreateProduct("Dress");
    clothe1->display();
    clothe2->display();

    delete shoe1;
    delete shoe2;
    return 0;
}
